# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import json


class DemoPipeline:
    def open_spider(self, spider):
        # 打开文件，使用追加模式
        self.file = open('output.txt', 'a', encoding='utf-8')

    def close_spider(self, spider):
        # 关闭文件
        self.file.close()

    def process_item(self, item, spider):
        print(f"正在处理管道数据：{item}")
        # 将Item转换为JSON字符串，并写入文件
        line = json.dumps(ItemAdapter(item).asdict(), ensure_ascii=False) + "\n"
        self.file.write(line)
        return item
